<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // var p = new Promise(function (resolve, reject) {
    //     resolve(1);
    // })
    // console.log(p);  // {fulfilled:1}


    Promise.myResolve = function (data) {
        var p = new Promise(function (resolve, reject) {
            if (data instanceof Promise) {//传入的参数是Promise对象
                return data;
            } else if (typeof data == "object" && data.then) {  //thenable对象(对象且有then方法)
                setTimeout(function () {
                    data.then(resolve, reject);
                })


            } else {
                resolve(data);
            }
        })
        return p;  //  {fulfilled:data}
    }

    // 如何快速得到一个成功状态,Promise对象(成功),并存储数据
    // Promise.resolve()
    // 1. 接收一个非Promise对象, 返回一个新的Promise对象,传入的参数作为该Promise对象的结果存储
    var p = Promise.resolve(1);
    console.log(p);

    // 2. 如果接收的是一个Promise对象, 直接返回该Promise对象
    var p1 = Promise.resolve(p);
    console.log(p1);
    console.log(p1 === p);

    // 3. 如果接收的参数是一个thenable对象  => 会将这个对象转为 Promise 对象，然后就立即执行thenable对象的then()方法。
    var p2 = Promise.resolve({
        then: function (resolve, reject) {
            resolve(3);
            // setTimeout(function () {
            //     resolve(3);
            //     console.log(p2);
            // }, 2000)
        }
    });
    console.log(p2);








</script>

</html>